package com.leo;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/**
 * @ClassName TableViewDemo
 * @Description
 * @Author wangss
 * @date 2020.06.13 18:52
 * @Version 1.0
 */
public class TableViewDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #fff2f2");

        Student s1 = new Student("小a", 10, 50d, false);
        Student s2 = new Student("小b", 11, 65d, true);
        Student s3 = new Student("小c", 11, 74d, true);
        Student s4 = new Student("小d", 9, 98d, true);

        ObservableList<Student> list = FXCollections.observableArrayList();
        list.addAll(s1, s2, s3, s4);

        TableView<Student> tableView = new TableView<>(list);
        TableColumn<Student, String> name = new TableColumn<>("姓名");

        // 加载数据的两种方式
        /*name.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Student, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<Student, String> param) {
                SimpleStringProperty nameStr = new SimpleStringProperty(param.getValue().getName());
                return nameStr;
            }
        });*/
        name.setCellValueFactory(new PropertyValueFactory<>("name"));

        TableColumn<Student, Integer> age = new TableColumn<>("年龄");
        age.setCellValueFactory(new PropertyValueFactory<>("age"));

        /*age.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Student, Number>, ObservableValue<Number>>() {
            @Override
            public ObservableValue<Number> call(TableColumn.CellDataFeatures<Student, Number> param) {
                SimpleIntegerProperty ageInt = new SimpleIntegerProperty(param.getValue().getAge());
                return ageInt;
            }
        });*/

        TableColumn<Student, Double> score = new TableColumn<>("成绩");
        /*score.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Student, Number>, ObservableValue<Number>>() {
            @Override
            public ObservableValue<Number> call(TableColumn.CellDataFeatures<Student, Number> param) {
                SimpleDoubleProperty scoreDou = new SimpleDoubleProperty(param.getValue().getScore());
                return scoreDou;
            }
        });*/
        score.setCellValueFactory(new PropertyValueFactory<>("score"));


        TableColumn<Student, Boolean> flag = new TableColumn<>("是否及格");
        /*flag.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Student, Boolean>, ObservableValue<Boolean>>() {
            @Override
            public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<Student, Boolean> param) {
                SimpleBooleanProperty flagBool = new SimpleBooleanProperty(param.getValue().isFlag());
                return flagBool;
            }
        });*/
        flag.setCellValueFactory(new PropertyValueFactory<>("flag"));

        tableView.getColumns().addAll(name, age, score, flag);

        Button button = new Button("button");

        AnchorPane.setTopAnchor(tableView, 50d);
        AnchorPane.setLeftAnchor(tableView, 50d);
        anchorPane.getChildren().addAll(button, tableView);

        Scene scene = new Scene(anchorPane);
        primaryStage.setScene(scene);
        primaryStage.setWidth(800d);
        primaryStage.setHeight(800d);
        primaryStage.setTitle("TableView");
        primaryStage.setResizable(false);
        primaryStage.show();

        button.setOnAction(event -> {
//            tableView.getItems().add(new Student("小f", 12, 59d, false));

            s1.setName("小改");
            tableView.refresh();
        });
    }
}
